package com.ldz.elastic.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.ldz.elastic.task.MyTestSimpleJob;
import com.ldz.elastic.task.MyTestSimpleJob1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class SimpleJobConfig {

    private static final String cron = "0/30 * * * * ?";

    private static final int shardingTotalCount = 1;

    private static final String shardingItemParameters = "0=A";

//    @Bean(initMethod = "init")
//    public JobScheduler simpleJobScheduler1(final MyTestSimpleJob1 simpleJob) {
//        LiteJobConfiguration liteJobConfiguration = getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters);
//        return new SpringJobScheduler(simpleJob, regCenter, liteJobConfiguration, getJobeventConfiguration());
//    }

    //  注册中心配置
    @Resource
    private ZookeeperRegistryCenter regCenter;

    @Resource
    private DruidDataSource dataSource;

    /**
     * 每个job都需要一个单独的JobScheduler对象来调度，可注册多个，传不同的SimpleJob实现类即可
     * @param simpleJob
     * @return
     */
    @Bean(initMethod = "init")
    public JobScheduler simpleJobScheduler(final MyTestSimpleJob simpleJob) {
        LiteJobConfiguration liteJobConfiguration = getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters);
        return new SpringJobScheduler(simpleJob, regCenter, liteJobConfiguration, getJobeventConfiguration());
    }

    /* 作业配置
     作业配置分为3级，分别是JobCoreConfiguration，JobTypeConfiguration和LiteJobConfiguration。
     LiteJobConfiguration使用JobTypeConfiguration，JobTypeConfiguration使用JobCoreConfiguration，层层嵌套。
     JobTypeConfiguration根据不同实现类型分为SimpleJobConfiguration，DataflowJobConfiguration和ScriptJobConfiguration。*/
    private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
        return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
                jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
    }

    // 将作业运行的痕迹进行持久化到DB的操作配置
    private JobEventConfiguration getJobeventConfiguration() {
        return new JobEventRdbConfiguration(dataSource);
    }

}